﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;


namespace Diary.WebAPI
{
    static public class Tools
    {
        static public int getQueryInt(this HttpRequest request, string key, int dv = -1)
        {
            int id = dv;
            string rid = request.HttpContext.Request.Query[key];
            int.TryParse(rid, out id);
            if (id <= 0) return dv;
            return id;
        }

        static public string getQuery(this HttpRequest request, string key, string dv = "")
        {
            string ret = request.HttpContext.Request.Query[key];
            if (string.IsNullOrEmpty(ret))
            {
                ret = dv;
            }
            return ret;
        }

        static public string getPostData(this HttpRequest request)
        {
            string content = "";
            System.IO.StreamReader reader = null;
            try
            {
                reader = new System.IO.StreamReader(request.Body, System.Text.Encoding.UTF8);
                Task<string> task = reader.ReadToEndAsync();
                content = task.Result;
            }
            catch (Exception ex)
            {

            }
            finally
            {
                if (reader != null) reader.Dispose();
            }

            return content;
        }
    }


}
